package com.plian.system.task.bank;

import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.fr.bank.BankAccountInfo;
import com.plian.system.entity.fr.bank.BankCurrentHistory;
import com.plian.system.service.fr.bank.IBankAccountInfoService;
import com.plian.system.service.fr.bank.IBankCurrentHistoryService;
//import com.plian.system.service.fr.bank.IBankTradeDetailService;
import com.plian.system.service.fr.indicatorswarning.IndicatorsWarningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @description: 资金取数 账户余额历史数据保存
 * @author: gangan
 * @create: 2020-07-23 14:43
 */
@Configuration
public class BankTask {

    private static final Logger log = LoggerFactory.getLogger(BankTask.class);

    @Autowired
    private IBankAccountInfoService bankAccountInfoService;

    @Autowired
    private IBankCurrentHistoryService bankCurrentHistoryService;

//    @Autowired TODO DEL
//    private IBankTradeDetailService bankTradeDetailService;

    @Autowired
    private IndicatorsWarningService indicatorsWarningService;

    @Autowired
    IBankAccountInfoService iBankAccountInfoService;

//    @Autowired TODO DEL
//    IBankOrgStorageService bankOrgStorageService;

    @Scheduled(cron = "0 0 4 * * ?")
    private void configureTasks() {
        log.info("资金取数 账户余额历史数据保存开始--------------> " + LocalDateTime.now());

        List<BankAccountInfo> bankAccountInfos = bankAccountInfoService.list();
        //获取昨天的日期
        Date yesterday = DateUtil.minusDays( DateUtil.now(), 1);

        if (CollectionUtil.isNotEmpty(bankAccountInfos)){
            List<BankCurrentHistory> bankCurrentHistories = new ArrayList<>();
            for (BankAccountInfo bankAccountInfo : bankAccountInfos){
                //过滤已销户账号
                if (!Optional.ofNullable(bankAccountInfo.getAccountStatus()).isPresent()
                    || bankAccountInfo.getAccountStatus() != 1){
                    continue;
                }

                BankCurrentHistory bankCurrentHistory = new BankCurrentHistory();
                BeanUtils.copyProperties(bankAccountInfo, bankCurrentHistory);

                bankCurrentHistory.setId(null);
                bankCurrentHistory.setCreateTime(yesterday);
                bankCurrentHistory.setIsDeleted(0);
                bankCurrentHistory.setUpdateTime(null);

                if (bankAccountInfo.getDepositType() == 6){
                    bankCurrentHistory.setLoanRate(bankAccountInfo.getRate());
                    bankCurrentHistory.setLoan(bankAccountInfo.getBalance());
                    bankCurrentHistory.setRate(null);
                    bankCurrentHistory.setBalance(null);
                }
                bankCurrentHistories.add(bankCurrentHistory);
            }

            if (CollectionUtil.isNotEmpty(bankCurrentHistories)){
                bankCurrentHistoryService.saveBatch(bankCurrentHistories);
            }
        }
        log.info("资金取数 账户余额历史数据保存结束--------------> " + LocalDateTime.now());
    }

//    @Scheduled(cron = "0 0 1 * * ?")
//    private void bankOrgStorageTasks() {
//        log.info("银行信息暂存数据保存开始--------------> " + LocalDateTime.now());
//        //获取昨天的日期
//        Date yesterday = DateUtil.minusDays( DateUtil.now(), 1);
//        bankOrgStorageService.saveFinancingCostRateByDay(DateUtil.format(yesterday,DateUtil.PATTERN_DATE));
//        bankOrgStorageService.saveOtherByDay(DateUtil.format(yesterday,DateUtil.PATTERN_DATE));
//
//        log.info("银行信息暂存保存结束--------------> " + LocalDateTime.now());
//    }

//    @Scheduled(cron = "0 0 23 * * ?") TODO DEL
//    //@Scheduled(cron = "* * * * * ?")
//    private void privateTopublictask(){
//        Date createTime = DateUtil.now();
//        String warningTime = DateUtil.format(createTime, "yyyy-MM");
//        PageData pageData = new PageData();
//        pageData.put("startTime",warningTime+"-01");
//        pageData.put("endTime",warningTime);
//        List<TradeStatisticsVO> isprivate = bankTradeDetailService.Isprivate(pageData);
//        for (TradeStatisticsVO vo :isprivate){
//            String id = UuidUtil.get32UUID();
//            String orgId = vo.getTopGroupId();
//            //49ade100f5374f2da87c774a7e41e0ae
//            String indicatorsId  = "55f90cb8c89e4963844ffe597e04c637";
//            Integer WarningType = 3;
//            Integer SourceType = 2;
//            int accountNumbers = vo.getTradeNumber();
//            if (accountNumbers > 0){
//                PageData pd = new PageData();
//                pd.put("id",id);
//                pd.put("orgId",orgId);
//                pd.put("warningTime",warningTime);
//                pd.put("indicatorsId",indicatorsId);
//                pd.put("warningType",WarningType);
//                //pd.put("warningTime","2020-11");
//                pd.put("sourceType",SourceType);
//                pd.put("createTime",createTime);
//                pd.put("warningData", String.valueOf(accountNumbers));
//                indicatorsWarningService.insertOrUpdateWarn(pd);
//            }
//        }
//    }

//   // @Scheduled(cron = "0 0 23 * * ?")
//    //@Scheduled(cron = "* * * * * ?")
//    private void accountNumtask(){
//        List<BankAccountInfoVO> warn = iBankAccountInfoService.warn();
//        for (BankAccountInfoVO vo :warn){
//            String id = UuidUtil.get32UUID();
//            String orgId = vo.getOrgId();
//            //49ade100f5374f2da87c774a7e41e0ae
//            String indicatorsId  = "49ade100f5374f2da87c774a7e41e0ae";
//            Integer WarningType = 3;
//            Integer SourceType = 4;
//            Date createTime = DateUtil.now();
//            String warningTime = DateUtil.format(createTime, "yyyy-MM");
//            int accountNumbers = vo.getAccountNumbers();
//            if (accountNumbers > 1){
//                PageData pd = new PageData();
//                pd.put("id",id);
//                pd.put("orgId",orgId);
//                pd.put("warningTime",warningTime);
//                pd.put("indicatorsId",indicatorsId);
//                pd.put("warningType",WarningType);
//                pd.put("sourceType",SourceType);
//                pd.put("createTime",createTime);
//                pd.put("warningData", String.valueOf(accountNumbers));
//                indicatorsWarningService.insertOrUpdateWarn(pd);
//            }
//        }
//    }
//
//    //@Scheduled(cron = "0 0 23 * * ?")
//    //@Scheduled(cron = "* */2 * * * ?")
//    private void accountTimetask(){
//        List<BankAccountInfoVO> warn = bankAccountInfoService.timeoutWarning();
//        HashMap<String, Integer> orgnum = new HashMap<>();
//        for (BankAccountInfoVO vo :warn){
//            String orgId = vo.getOrgId();
//            boolean b = orgnum.containsKey(orgId);
//            if (b){
//                Integer num = orgnum.get(orgId);
//                orgnum.put(orgId,num+1);
//            }else {
//                orgnum.put(orgId,1);
//            }
//        }
//        orgnum.remove(null);
//        Set<String> strings = orgnum.keySet();
//        Iterator<String> iterator = strings.iterator();
//        while (iterator.hasNext()){
//            String orgId = iterator.next();
//            Integer num = orgnum.get(orgId);
//            PageData pd = new PageData();
//            String id = UuidUtil.get32UUID();
//            String indicatorsId  = "7011aa8078ad4ca7a49dd185dbd0fd64";
//            Integer warningType = 3;
//            Integer sourceType = 3;
//            Date createTime = DateUtil.now();
//            String warningTime = DateUtil.format(createTime, "yyyy-MM");
//            pd.put("id",id);
//            pd.put("orgId",orgId);
//            pd.put("warningTime",warningTime);
//            pd.put("indicatorsId",indicatorsId);
//            pd.put("warningType",warningType);
//            pd.put("sourceType",sourceType);
//            pd.put("createTime",createTime);
//            pd.put("warningData", String.valueOf(num));
//            indicatorsWarningService.insertOrUpdateWarn(pd);
//        }
//    }
}
